﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EEPManager
{
    public partial class frmSecurityMainUsersOverride : Form
    {
        public frmSecurityMainUsersOverride()
        {
            InitializeComponent();
        }

        private void btnResetPWD_Click(object sender, EventArgs e)
        {
            String PWD = "";
            try
            {
                string Sql = "UPDATE USERS SET PWD='" + PWD + "', LASTDATE='' where USERID='" + (infoBindingSource1.Current as DataRowView).Row["USERID"].ToString() + "'";


                Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse",
                       Sql, true, Srvtools.CliUtils.fCurrentProject);
                MessageBox.Show("Reset succeed!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAMUser_Click(object sender, EventArgs e)
        {
            string strUserID = "";
            string strUserName = "";
            int index = -1;

            if (infoDataGridView1.SelectedRows.Count == 1)
            {
                index = infoDataGridView1.SelectedRows[0].Index;
                strUserID = infoDataGridView1.Rows[index].Cells["uSERIDDataGridViewTextBoxColumn"].Value.ToString();
                strUserName = infoDataGridView1.Rows[index].Cells["uSERNAMEDataGridViewTextBoxColumn"].Value.ToString();
            }
            else if (infoDataGridView1.SelectedCells.Count == 1)
            {
                index = infoDataGridView1.SelectedCells[0].RowIndex;
                strUserID = infoDataGridView1.Rows[index].Cells["uSERIDDataGridViewTextBoxColumn"].Value.ToString();
                strUserName = infoDataGridView1.Rows[index].Cells["uSERNAMEDataGridViewTextBoxColumn"].Value.ToString();
            }
            if (strUserID != "")
            {
                Srvtools.UGControl g = new Srvtools.UGControl();
                g.AMUser(infoDataSet1, strUserID, strUserName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Filter = "";
            if (textBox1.Text != "")
            {
                Filter += " And USERNAME LIKE '%" + textBox1.Text.Trim() + "%' ";
            }
            if (usasComboBox1.SelectedValue != null && usasComboBox1.SelectedValue.ToString() != "")
            {
                Filter += " And Department.DepartmentId like '" + usasComboBox1.SelectedValue.ToString() + "%' ";
            }

            infoDataSet1.SetWhere("1=1 " + Filter);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            usasComboBox1.SelectedIndex = -1;
            infoDataSet1.SetWhere("1=1");
        }

        private void infoDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (infoDataGridView1.Columns[e.ColumnIndex].Name == "GpSelect")
            {
                frmGroupListByUser glbu = new frmGroupListByUser(
                    infoDataGridView1.Rows[e.RowIndex].Cells["uSERIDDataGridViewTextBoxColumn"].Value.ToString(),
                    infoDataGridView1.Rows[e.RowIndex].Cells["uSERNAMEDataGridViewTextBoxColumn"].Value.ToString());
                glbu.ShowDialog();
            }
        }
    }
}
